<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    共用体类型 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						共用体类型
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/28 01:52 上午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p>使几个不同的变量共占同一段内存的结构称为<code>共用体</code>类型的结构</p>
<h2><a id="%E5%AE%9A%E4%B9%89%E5%85%B1%E7%94%A8%E4%BD%93%E7%B1%BB%E5%9E%8B%E5%8F%98%E9%87%8F%E7%9A%84%E4%B8%80%E8%88%AC%E5%BD%A2%E5%BC%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>定义共用体类型变量的一般形式</h2>
<pre class="line-numbers"><code class="language-c">union 共用体名
{
    成员表列
} 变量表列;
</code></pre>
<h2><a id="%E5%85%B1%E7%94%A8%E4%BD%93%E5%92%8C%E7%BB%93%E6%9E%84%E4%BD%93%E7%9A%84%E5%8C%BA%E5%88%AB" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>共用体和结构体的区别</h2>
<h3><a id="%E7%BB%93%E6%9E%84%E4%BD%93" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>结构体</h3>
<ul>
<li>结构体变量所占内存长度是各成员占的内存长度之和，每个成员分别占有自己的内存单元</li>
<li>任何时候使用其中的任意成员</li>
</ul>
<h3><a id="%E5%85%B1%E7%94%A8%E4%BD%93" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>共用体</h3>
<ul>
<li>共用体变量所占的内存长度等于最长的成员的长度</li>
<li>只能在某一时刻使用其中的一个成员</li>
</ul>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>